<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  
class Excel  
{  
    // public variable to store an instance of the PHPExcel object  
    var $workbook = '';  
  
    // class constructor  
    function Excel()  
    {  
        // get the library directory  
  		$file_url = __FILE__;
		// Cleanup backslashes for Windows
		// e.g. C:/xampp/htdocs/system/application/libraries/Excel.php
		$library_dir = str_replace('\\','/', $file_url);
		// e.g. C:/xampp/htdocs/system_heckyes/application/libraries
		$library_dir = substr($library_dir, 0, strrpos($library_dir, '/'));
        // adjust the path variable  
        set_include_path(get_include_path() . PATH_SEPARATOR . $library_dir . '/phpexcel/');  
  
        // load the phpexcel class  
        require_once($library_dir . '/phpexcel/PHPExcel.php');  
  
        // load the phpexcel IOFactory stuff for output  
        require_once('PHPExcel/IOFactory.php');  
  
        // instantiate the object  
        return $this->new_workbook();  
  
    }
  
    // new workbook function  
    function new_workbook() {  
  
        // reset the workbook to a new object  
        unset($this->workbook);  
        $this->workbook = new PHPExcel();  
  
        if(isset($this->workbook)) {  
            return TRUE;  
        } else {  
            return FALSE;  
        }  
  
    }
  
    // function to output the file  
    function send_to_browser($filename = "workbook.xls", $format = 'Excel5'){  
  
        // load the appropriate IO Factory writer  
        $objWriter = PHPExcel_IOFactory::createWriter($this->workbook, $format);  
  		$objWriter->setTempDir($_SERVER['DOCUMENT_ROOT']);
  		
        // output the appropriate headers  
        header('Pragma: public');  
        header('Expires: 0');  
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
        header('Content-Type: application/force-download');  
        header('Content-Type: application/octet-stream');  
        header('Content-Type: application/download');  
        header("Content-Disposition: attachment;filename={$filename}");  
        header('Content-Transfer-Encoding: binary');  
  
        // output the file  
        $objWriter->save('php://output');  
  
        return;
    }  
}  
  
?>